package cn.com.surker.workcon.service.fengdu;

import cn.com.surker.base.system.entity.SysLine;
import cn.com.surker.enums.production.invoice.InvoiceProcessEnum;
import cn.com.surker.enums.production.invoice.InvoiceSendTypeEnum;
import cn.com.surker.production.entity.ProductionIndentDispatch;
import cn.com.surker.util.DateUtil;
import cn.com.surker.util.StringUtils;
import cn.com.surker.workcon.common.GKJDBCService;
import cn.com.surker.workcon.service.ProduceService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author luotao
 * @date 2024/12/18
 */
@Slf4j
public class ProduceServiceImpl extends GKJDBCService implements ProduceService {

	@Override
	public int insert(Long lineId, ProductionIndentDispatch dispatch) {
		if (lineId == null || dispatch == null) {
			return -1;
		}
		SysLine line = getLine(lineId);
		JdbcTemplate lineTemplate = getLineJdbcTemp(line);
		//	没有数据源
		if (lineTemplate == null) {
			return -1;
		}
		//	任务单编号
		String indentCode = dispatch.getIndentCode();
		if (StringUtils.isBlank(indentCode)) {
			log.info("派单{}没有任务单数据", dispatch.getCode());
			return -1;
		}
		//	根据任务单编号,查询是否存在上传数据
		String queryOldTask = "select id from PTaskList where PTaskListNo = '" + indentCode + "'";
		//	查询数据
		List<Map<String, Object>> oldTasks = lineTemplate.queryForList(queryOldTask);
		if (oldTasks == null || oldTasks.isEmpty() || oldTasks.get(0) == null) {
			//	没有上传数据
			log.info("任务单{}没有查询到工控数据", indentCode);
			return -1;
		}
		Object pTaskListID = oldTasks.get(0).get("id");
		//	查询最最新的一条关联配合比数据
		String queryPhbSql = "select id from PTaskFormula where PTaskListID = ? order by CreateTime desc";
		Object[] phbParam = {pTaskListID};
		List<Map<String, Object>> phbList = lineTemplate.queryForList(queryPhbSql, phbParam);
		if (phbList == null || phbList.isEmpty() || phbList.get(0) == null) {
			log.info("任务单{}关联的工控任务单id{}没有查询到关联配合比数据", indentCode, pTaskListID);
		}
		Object TaskFormulaID = phbList.get(0).get("id");
		//	派单编码
		String ProductRegNo = dispatch.getCode();
		//	派单序号
		Long PRegOrder = dispatch.getId();
		//	车号
		String CarID = dispatch.getCarCode();
		//	司机
		String Driver = dispatch.getDriver();
		//	砼方量
		Double ConcreteSumNum = 0d;
		//	砂浆方量
		Double MortarNum = 0d;
		Double uploadStere = dispatch.getUploadStere();
		Integer uploadType = dispatch.getUploadType();
		if (InvoiceSendTypeEnum.CONCRETE.getCode().equals(uploadType)) {
			ConcreteSumNum = uploadStere;
		} else if (InvoiceSendTypeEnum.SAND.getCode().equals(uploadType)) {
			ConcreteSumNum = uploadStere;
			MortarNum = uploadStere;
		} else {
			//	默认为砼方量
			ConcreteSumNum = uploadStere;
		}
		//	上传时间
		Date datTime = dispatch.getUploadTime();
		//	添加派单数据sql
		String insertSql = "insert into ProductRegist (ProductRegNo,PRegOrder,TaskFormulaID,CarID,Driver,ConcreteSumNum,MortarNum,StartTime,EndTime,CreateTime,Status,SendTime) " +
			" values (?,?,?,?,?,?,?,?,?,?,?,?) ";
		//	拼接参数
		Object[] pdParams = {ProductRegNo, PRegOrder, TaskFormulaID, CarID, Driver, ConcreteSumNum, MortarNum, datTime, datTime, datTime, 0, datTime};
		//	开启事务
		beginTransaction(lineId);
		//	插入数据
		int update = lineTemplate.update(insertSql, pdParams);
		return update;
	}

	public Integer queryByDispatchId(Long lineId, Long dispatchId) {
		if (lineId == null || dispatchId == null) {
			return null;
		}
		SysLine line = getLine(lineId);
		JdbcTemplate lineTemplate = getLineJdbcTemp(line);
		//	没有数据源
		if (lineTemplate == null) {
			return null;
		}
		Integer prostats = InvoiceProcessEnum.QUERN_RECEIVE.getCode();
		//	查询工控机的生产状态
		String querySql = "select status from ProductRegist where PRegOrder = " + dispatchId;
		//	查询数据
		List<Map<String, Object>> maps = lineTemplate.queryForList(querySql);
		if (maps == null || maps.isEmpty()) {
			return null;
		}
		Map<String, Object> map = maps.get(0);
		if (map == null) {
			return null;
		}
		Object status = map.get("status");
		if (status == null) {
			return null;
		}
		prostats = InvoiceProcessEnum.QUERN_PRO.getCode();
		Integer tStatus = (Integer) status;
		if (tStatus > 0) {
			//	生产中
			prostats = InvoiceProcessEnum.PRO.getCode();
		} else if (tStatus < 0) {
			//	删除
			prostats = InvoiceProcessEnum.DEL.getCode();
		}
		/*ProductionIndentDispatch erpDisPatch = new ProductionIndentDispatch();
		erpDisPatch.setProStatus(prostats);*/
		return prostats;
	}

	@Override
	public Boolean disHasDel(Long lineId, Long dispatchId) {
		if (lineId == null || dispatchId == null) {
			return null;
		}
		SysLine line = getLine(lineId);
		JdbcTemplate lineTemplate = getLineJdbcTemp(line);
		//	没有数据源
		if (lineTemplate == null) {
			return null;
		}
		//	查询工控机的生产状态
		String querySql = "select * from ProductRegist where PRegOrder = " + dispatchId;
		//	查询数据
		List<Map<String, Object>> maps = lineTemplate.queryForList(querySql);
		if (maps == null || maps.isEmpty()) {
			return null;
		}
		Map<String, Object> map = maps.get(0);
		if (map == null) {
			return null;
		}
		Object status = map.get("status");
		if (status == null) {
			return null;
		}
		Integer tStatus = (Integer) status;
		if (tStatus < 0) {
			//	删除
			log.info("工控机删除了数据:查询的原始数据:{" + JSONObject.toJSONString(map) + "},传入的参数:" + dispatchId + "操作时间:" + DateUtil.convertString(new Date()));
			return true;
		} else {
			return false;
		}
	}

	@Override
	public Integer delDispatch(Long lineId, Long dispatchId) {
		if (lineId == null || dispatchId == null) {
			return -1;
		}
		SysLine line = getLine(lineId);
		JdbcTemplate lineTemplate = getLineJdbcTemp(line);
		//	没有数据源
		if (lineTemplate == null) {
			return -1;
		}
		//	删除sql
		String delSql = "delete from ProductRegist where PRegOrder = ? and flag != 0";
		Object[] params = {dispatchId};
		//	开启事务
		beginTransaction(lineId);
		int update = lineTemplate.update(delSql, params);
		return update;
	}
}


